package com.androidx.p013lv.mine.activity;

import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.androidx.p013lv.base.bean.UserInfo;
import com.androidx.p013lv.base.bean.event.LoginEvent;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.http.HttpUtils;
import com.androidx.p013lv.base.interfaces.Key;
import com.androidx.p013lv.base.p015ui.BaseActivity;
import com.androidx.p013lv.base.utils.DeviceIDUtils;
import com.androidx.p013lv.base.utils.NetUtil;
import com.androidx.p013lv.base.utils.SpUtils;
import com.androidx.p013lv.base.utils.ToastUtils;
import com.androidx.p013lv.base.utils.UiUtils;
import com.androidx.p013lv.mine.C1207R;
import com.androidx.p013lv.mine.bean.MobileAction;
import com.androidx.p013lv.mine.databinding.ActivityLoginLayoutBinding;
import com.androidx.p013lv.mine.model.LoginModel;
import com.gyf.immersionbar.ImmersionBar;
import java.util.concurrent.TimeUnit;
import org.greenrobot.eventbus.EventBus;
import org.slf4j.Marker;
import p023io.reactivex.Observable;
import p023io.reactivex.android.schedulers.AndroidSchedulers;
import p023io.reactivex.disposables.Disposable;
import p023io.reactivex.functions.Action;
import p023io.reactivex.functions.Consumer;
import p023io.reactivex.schedulers.Schedulers;

/* renamed from: com.androidx.lv.mine.activity.LoginActivity */
public class LoginActivity extends BaseActivity<ActivityLoginLayoutBinding> {
    MobileAction action;
    public int actionType;
    Disposable disposable;
    private boolean isStart;
    private LoginModel model;
    public int stationType;
    private UserInfo userInfo;

    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public int setLayout() {
        return C1207R.C1212layout.activity_login_layout;
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initView() {
        super.initView();
        this.model = (LoginModel) new ViewModelProvider(this).get(LoginModel.class);
        ((ActivityLoginLayoutBinding) this.binding).setClick(new OnClick());
        ((ActivityLoginLayoutBinding) this.binding).setStatus(0);
        ((ActivityLoginLayoutBinding) this.binding).setBtnStatus(0);
        MobileAction mobileAction = new MobileAction();
        this.action = mobileAction;
        mobileAction.setActionType(this.actionType);
        ((ActivityLoginLayoutBinding) this.binding).setAction(this.action);
        this.userInfo = SpUtils.getInstance().getUserInfo();
        ((ActivityLoginLayoutBinding) this.binding).edLoginInputPhone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            /* class com.androidx.p013lv.mine.activity.LoginActivity.View$OnFocusChangeListenerC12371 */

            public void onFocusChange(View view, boolean z) {
                if (!z && LoginActivity.this.stationType != 1) {
                    LoginActivity.this.checkLoginStatus();
                }
            }
        });
        ((ActivityLoginLayoutBinding) this.binding).edLoginInputPhone.addTextChangedListener(new TextWatcher() {
            /* class com.androidx.p013lv.mine.activity.LoginActivity.C12392 */

            public void afterTextChanged(Editable editable) {
            }

            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                int i4 = 1;
                if (!LoginActivity.this.isStart) {
                    ((ActivityLoginLayoutBinding) LoginActivity.this.binding).setStatus(Integer.valueOf(charSequence.length() > 0 ? 1 : 0));
                }
                if (charSequence.length() <= 0) {
                    i4 = 0;
                }
                ((ActivityLoginLayoutBinding) LoginActivity.this.binding).setBtnStatus(Integer.valueOf(i4));
            }
        });
        ((ActivityLoginLayoutBinding) this.binding).edLoginInputCode.addTextChangedListener(new TextWatcher() {
            /* class com.androidx.p013lv.mine.activity.LoginActivity.C12403 */

            public void afterTextChanged(Editable editable) {
            }

            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                if (!LoginActivity.this.isStart) {
                    ((ActivityLoginLayoutBinding) LoginActivity.this.binding).setStatus(Integer.valueOf(charSequence.length() > 0 ? 3 : 1));
                }
            }
        });
        this.model.dataAction().observe(this, new Observer<BaseRes<MobileAction>>() {
            /* class com.androidx.p013lv.mine.activity.LoginActivity.C12414 */

            public void onChanged(BaseRes<MobileAction> baseRes) {
                if (baseRes.getCode() != 200 || baseRes.getData() == null) {
                    ToastUtils.getInstance().showWrong(baseRes.getMsg());
                    return;
                }
                LoginActivity.this.action = baseRes.getData();
                ((ActivityLoginLayoutBinding) LoginActivity.this.binding).setAction(LoginActivity.this.action);
            }
        });
        this.model.dataCode().observe(this, new Observer<BaseRes<String>>() {
            /* class com.androidx.p013lv.mine.activity.LoginActivity.C12425 */

            public void onChanged(BaseRes<String> baseRes) {
                if (baseRes.getCode() == 200) {
                    LoginActivity.this.startTimer();
                    ToastUtils.getInstance().showCorrect("發送成功");
                    return;
                }
                ToastUtils.getInstance().showWeak("發送失敗");
            }
        });
        this.model.dataBindPhone().observe(this, new Observer<BaseRes<String>>() {
            /* class com.androidx.p013lv.mine.activity.LoginActivity.C12436 */

            public void onChanged(BaseRes<String> baseRes) {
                if (baseRes.getCode() == 200) {
                    LoginActivity.this.userInfo.setMobile(baseRes.getData());
                    SpUtils.getInstance().setUserInfo(LoginActivity.this.userInfo);
                    ToastUtils.getInstance().showCorrect("綁定成功");
                    LoginActivity.this.finish();
                    return;
                }
                ToastUtils.getInstance().showCorrect(baseRes.getMsg());
            }
        });
        this.model.data().observe(this, new Observer<BaseRes<UserInfo>>() {
            /* class com.androidx.p013lv.mine.activity.LoginActivity.C12447 */

            public void onChanged(BaseRes<UserInfo> baseRes) {
                if (baseRes.getCode() == 200) {
                    LoginActivity.this.userInfo = baseRes.getData();
                    SpUtils.getInstance().put(Key.TOKEN, LoginActivity.this.userInfo.getToken());
                    SpUtils.getInstance().setUserInfo(LoginActivity.this.userInfo);
                    if (LoginActivity.this.stationType == 1) {
                        EventBus.getDefault().post(new LoginEvent());
                    }
                    LoginActivity.this.finish();
                    return;
                }
                ToastUtils.getInstance().showSigh(baseRes.getMsg());
            }
        });
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).titleBarMarginTop(((ActivityLoginLayoutBinding) this.binding).toolbar).init();
    }

    public String getAreaCode() {
        if (TextUtils.isEmpty(((ActivityLoginLayoutBinding) this.binding).edAreaCode.getText().toString().trim())) {
            return "";
        }
        return Marker.ANY_NON_NULL_MARKER + ((ActivityLoginLayoutBinding) this.binding).edAreaCode.getText().toString().trim();
    }

    public String getPhoneNum() {
        return ((ActivityLoginLayoutBinding) this.binding).edLoginInputPhone.getText().toString().trim();
    }

    public String getLoginCode() {
        return ((ActivityLoginLayoutBinding) this.binding).edLoginInputCode.getText().toString().trim();
    }

    public void checkLoginStatus() {
        if (TextUtils.isEmpty(getAreaCode())) {
            ToastUtils.getInstance().showSigh("請輸入手機區號");
        } else if (TextUtils.isEmpty(getPhoneNum())) {
            ToastUtils.getInstance().showSigh("請輸入手機碼號");
        } else {
            String str = getAreaCode() + getPhoneNum();
            if (str.equals(this.userInfo.getMobile())) {
                MobileAction mobileAction = new MobileAction();
                this.action = mobileAction;
                mobileAction.setActionType(0);
                ((ActivityLoginLayoutBinding) this.binding).btLoginCommit.setVisibility(0);
                ((ActivityLoginLayoutBinding) this.binding).btLoginCommit.setText(this.action.getDes());
                return;
            }
            this.model.mobileAction(str);
        }
    }

    public void getCaptcha() {
        if (!NetUtil.isNetworkAvailable()) {
            ToastUtils.getInstance().showWrong(UiUtils.getString(C1207R.string.hit_no_net));
        } else if (TextUtils.isEmpty(getAreaCode())) {
            ToastUtils.getInstance().showSigh("請輸入手機區號");
        } else if (TextUtils.isEmpty(getPhoneNum())) {
            ToastUtils.getInstance().showSigh("請輸入手機碼號");
        } else {
            String str = getAreaCode() + getPhoneNum();
            if (str.equals(this.userInfo.getMobile())) {
                MobileAction mobileAction = new MobileAction();
                this.action = mobileAction;
                mobileAction.setActionType(0);
                ((ActivityLoginLayoutBinding) this.binding).setAction(this.action);
            }
            this.model.getPhoneCode(str, this);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void login() {
        if (!NetUtil.isNetworkAvailable()) {
            ToastUtils.getInstance().showWrong(UiUtils.getString(C1207R.string.hit_no_net));
        } else if (TextUtils.isEmpty(getAreaCode())) {
            ToastUtils.getInstance().showSigh("請輸入手機區號");
        } else if (TextUtils.isEmpty(getPhoneNum())) {
            ToastUtils.getInstance().showSigh("請輸入手機碼號");
        } else if (TextUtils.isEmpty(getLoginCode())) {
            ToastUtils.getInstance().showSigh("請輸入手機驗證碼");
        } else {
            this.model.login(getAreaCode() + getPhoneNum(), getLoginCode(), DeviceIDUtils.getUniqueId(this), this);
        }
    }

    public void bindMobile() {
        if (!NetUtil.isNetworkAvailable()) {
            ToastUtils.getInstance().showWrong(UiUtils.getString(C1207R.string.hit_no_net));
        } else if (TextUtils.isEmpty(getAreaCode())) {
            ToastUtils.getInstance().showSigh("請輸入手機區號");
        } else if (TextUtils.isEmpty(getPhoneNum())) {
            ToastUtils.getInstance().showSigh("請輸入手機碼號");
        } else if (TextUtils.isEmpty(getLoginCode())) {
            ToastUtils.getInstance().showSigh("請輸入手機驗證碼");
        } else {
            String str = getAreaCode() + getPhoneNum();
            if (str.equals(this.userInfo.getMobile())) {
                ToastUtils.getInstance().showSigh("此號碼您正在使用");
            } else {
                this.model.bindMobile(str, getLoginCode());
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void startTimer() {
        this.isStart = true;
        ((ActivityLoginLayoutBinding) this.binding).setStatus(2);
        this.disposable = Observable.interval(0, 1, TimeUnit.SECONDS).take(60).subscribeOn(Schedulers.m1118io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Long>() {
            /* class com.androidx.p013lv.mine.activity.LoginActivity.C12458 */

            public void accept(Long l) throws Exception {
                ((ActivityLoginLayoutBinding) LoginActivity.this.binding).setCodeStatusTxt((60 - l.longValue()) + "s");
            }
        }, new Consumer<Throwable>() {
            /* class com.androidx.p013lv.mine.activity.LoginActivity.C12469 */

            public void accept(Throwable th) throws Exception {
                LoginActivity.this.isStart = false;
            }
        }, new Action() {
            /* class com.androidx.p013lv.mine.activity.LoginActivity.C123810 */

            @Override // p023io.reactivex.functions.Action
            public void run() throws Exception {
                LoginActivity.this.isStart = false;
                if (TextUtils.isEmpty(LoginActivity.this.getLoginCode())) {
                    ((ActivityLoginLayoutBinding) LoginActivity.this.binding).setStatus(1);
                } else {
                    ((ActivityLoginLayoutBinding) LoginActivity.this.binding).setStatus(3);
                }
            }
        });
    }

    public static void sendCodeStatus(TextView textView, int i) {
        if (i == 0) {
            textView.setClickable(false);
            textView.setBackgroundResource(C1207R.C1209drawable.mine_bg_phone_un_code);
            textView.setTextColor(Color.parseColor("#b1b1b1"));
            textView.setText("獲取驗證碼");
            textView.setTextSize(14.0f);
        } else if (i == 1) {
            textView.setClickable(true);
            textView.setBackgroundResource(C1207R.C1209drawable.mine_bg_phone_code);
            textView.setTextColor(Color.parseColor("#b1b1b1"));
            textView.setText("獲取驗證碼");
            textView.setTextSize(14.0f);
        } else if (i != 2) {
            textView.setClickable(true);
            textView.setBackground(null);
            textView.setTextColor(Color.parseColor("#b1b1b1"));
            textView.setText("重新獲取");
            textView.setTextSize(14.0f);
        } else {
            textView.setClickable(false);
            textView.setBackground(null);
            textView.setTextColor(Color.parseColor("#b1b1b1"));
            textView.setTextSize(18.0f);
        }
    }

    /* access modifiers changed from: protected */
    @Override // androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, com.androidx.p013lv.base.p015ui.BaseActivity
    public void onDestroy() {
        HttpUtils.getInsatance().cancelTag("login");
        HttpUtils.getInsatance().cancelTag("getCaptcha");
        HttpUtils.getInsatance().cancelTag("bindMobile");
        super.onDestroy();
    }

    /* renamed from: com.androidx.lv.mine.activity.LoginActivity$OnClick */
    public class OnClick {
        public OnClick() {
        }

        public void btnBack() {
            LoginActivity.this.finish();
        }

        public void requestCode() {
            LoginActivity.this.getCaptcha();
        }

        public void btnCommit() {
            if (LoginActivity.this.action.getActionType() == 0 || LoginActivity.this.action.getActionType() == 1) {
                LoginActivity.this.login();
            } else {
                LoginActivity.this.bindMobile();
            }
        }

        public void clearPhone() {
            ((ActivityLoginLayoutBinding) LoginActivity.this.binding).edLoginInputPhone.setText("");
            ((ActivityLoginLayoutBinding) LoginActivity.this.binding).edLoginInputCode.setText("");
        }
    }
}
